<template>
  <view class="contenting">
    <!-- <Test v-if="!isTime"></Test> -->
    <view>
      <Search></Search>
      <Preference :preferdata="preferdata"></Preference>
      <Title></Title>
      <view @click="poll()" :catchtouchmove="true">
        <Delicacy id="boxFixed" :class="{ isfixed: isfixed }"></Delicacy>
      </view>
      <view style="height: 50upx"></view>
      <Takeout :takeshop="takeshop"></Takeout>
    </view>
  </view>
</template>

<script>
//引入子组件
import Search from "./components/search.vue";
import Preference from "./components/preference.vue";
import Title from "./components/title.vue";
import Delicacy from "./components/delicacy.vue";
import Takeout from "./components/takeout.vue";
// import Test  from './components/test.vue'

//vuex仓库
import {mapState} from 'vuex'
//引入请求函数
import { listing } from "../../api/api.js";
//引入接口apl
import { getpreferurl, wxshopurl } from "../../api/request.js";
export default {
  components: {
    Search,
    Preference,
    Title,
    Delicacy,
    Takeout,
    // Test
  },
  data() {
    return {
      // isTime:false,
      title: "Hello",
      menutop: 0,
      rect: "",
      isfixed: false,
      //为你优选
      preferdata: [],
      //商家列表
      takeshop: [],
    };
  },
  onLoad() {
    // this.isTime = this.getTime();
    //监听筛选组件距离顶部的距离
    const query = wx.createSelectorQuery();
    query.select("#boxFixed").boundingClientRect();
    query.exec((res) => {
      this.menutop = res[0].top;
      // this.menutop = res[1].scrollTop;
    });
  },
  //监听页面滚动距离
  onPageScroll(e) {
    this.rect = e.scrollTop;
  },
  //生命周期钩子
  mounted() {
    this.preference();
  },
  computed: {
    // ... 是ES6的扩展运算符
    ...mapState(['screendata']),
    //取出筛选数据
    count(){
    	this.takeshop = this.screendata.screenarr
    },
    //监听筛选组件，是否置顶
    namePage() {
      if (this.rect > this.menutop) {
        this.isfixed = true;
      } else {
        this.isfixed = false;
      }
    },
  },
  methods: {
    //为你忧选   https://meituan.thexxdd.cn/api/forshop/getprefer   get
    //获取商家数据
    preference() {
      //批量并发请求
      Promise.all([listing(getpreferurl), listing(wxshopurl)])
        .then((res) => {
          //猜你喜欢
          this.preferdata = res[0].data;
          //商家列表
          this.takeshop = res[1].data;
        })
        .catch((err) => {
          console.log(err);
        });
    },

    //回到顶部
    poll() {
      wx.pageScrollTo({
        scrollTop: this.menutop,
        duration: 300,
      });
    },
    // getTime() {
    // 	var date = new Date(),
    // 	year = date.getFullYear(),
    // 	month = date.getMonth() + 1,
    // 	day = date.getDate(),
    // 	hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
    // 	minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
    // 	second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    // 	month >= 1 && month <= 9 ? (month = "0" + month) : "";
    // 	day >= 0 && day <= 9 ? (day = "0" + day) : "";
    // 	var newTime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;

    // 	var time = '2022-03-18 20:00:16';
    // 	var isTime = newTime > time ? true :false;
    // 	return isTime;
    // }
  },
};
</script>

<style scoped>
.contenting {
  margin: 0 15upx;
}

.isfixed {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
}
</style>
